//
//  SGGrounpViewController.m
//  朋友圈
//
//  Created by gzz on 2017/6/24.
//  Copyright © 2017年 gzz. All rights reserved.
//

#import "SGGrounpViewController.h"
#import "SGGroupTableViewCell.h"
#import "SGPhotoList.h"
#import "SGCollectionVIewController.h"

@interface SGGrounpViewController ()<UITableViewDelegate,UITableViewDataSource,PHPhotoLibraryChangeObserver>

//全部图片列表
@property(nonatomic,strong) NSMutableArray<PHAsset *> *contentArray;
//
@property (nonatomic,strong)NSMutableArray<SGPhotoAblumList *>*listArray;
@property(nonatomic,strong)UITableView *tableView;


@end

@implementation SGGrounpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"图片或视频";
//    数组初始化
    _listArray = [NSMutableArray new];
    _contentArray = [NSMutableArray new];
    
//    判断是否可以访问相册
    
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
        NSLog(@"对不起，你暂无访问权限");
        
    }
    else
    {
//        移除数组里原来所有数据
        [_listArray removeAllObjects];
        
//        从相册里加载图片
        [_listArray addObjectsFromArray:[[SGPhotoList sharePhotoTools]getPhotoAblumList]];
//        tableView 刷新数据
        
        [self.tableView reloadData];
        
    }
    
    
    
    
    [self tableView];
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(backHome)];
}

#pragma mark -- backHome
-(void)backHome
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark TableView

-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}

-(void)photoLibraryDidChange:(PHChange *)changeInstance
{
    __weak SGGrounpViewController *weakSelf = self;
    dispatch_sync(dispatch_get_main_queue(), ^{
        
        __strong SGGrounpViewController  *strongSelf =weakSelf;
        
        [_listArray removeAllObjects];
        [_listArray addObjectsFromArray:[[SGPhotoList sharePhotoTools]getPhotoAblumList]];
        
        [strongSelf.tableView reloadData];
    
    });
}
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor greenColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        
    }
    return _tableView;
}
#pragma mark UITableviewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    
    SGGroupTableViewCell *SGCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!SGCell) {
        SGCell = [[SGGroupTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    
    SGPhotoAblumList *ablumList = _listArray[indexPath.row];
    SGCell.selectionStyle = UITableViewCellEditingStyleNone;
    [SGCell configUI:ablumList];
    return SGCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SGCollectionVIewController *collection = [[SGCollectionVIewController alloc]init];
    
    
    __weak SGGrounpViewController   *weakSelf = self;
    collection.imageBlockArray = ^(NSMutableArray<PHAsset *>*array){
        __strong SGGrounpViewController *strongSelf = weakSelf;
        if (array) {
            strongSelf.backImageArray(array);
        }
    };
    collection.maxChooseNumber = self.maxChooseNUmber;
    
    collection.ablum = _listArray[indexPath.row];
    
    
    [self.navigationController pushViewController:collection animated:YES];
}




@end
